package cn.iocoder.yudao.module.member.controller.admin.device;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.device.recrod.MemberDeviceRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.device.recrod.MemberDeviceRecordRespVO;
import cn.iocoder.yudao.module.member.convert.device.MemberDeviceRecordConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.device.MemberDeviceRecordDO;
import cn.iocoder.yudao.module.member.dal.dataobject.user.MemberUserDO;
import cn.iocoder.yudao.module.member.service.device.MemberDeviceRecordService;
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Set;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

@Tag(name = "管理后台 - 设备管理")
@RestController
@RequestMapping("/member/device/record")
@Validated
public class MemberDeviceRecordController {

    @Resource
    private MemberDeviceRecordService deviceRecordService;

    @Resource
    private MemberUserService memberUserService;

    @GetMapping("/page")
    @Operation(summary = "获得用户设备记录分页")
    @PreAuthorize("@ss.hasPermission('device:record:query')")
    public CommonResult<PageResult<MemberDeviceRecordRespVO>> getPointRecordPage(@Valid MemberDeviceRecordPageReqVO pageVO) {
        // 执行分页查询
        // 根据用户昵称查询出用户 ids
        Set<Long> userIds;
        if (StringUtils.isNotBlank(pageVO.getNickname())) {
            List<MemberUserDO> users = memberUserService.getUserListByNickname(pageVO.getNickname());
            if (CollectionUtils.isEmpty(users)) {
                return success(PageResult.empty());
            }
            userIds = convertSet(users, MemberUserDO::getId);
            pageVO.setUserIds(userIds);
        }
        PageResult<MemberDeviceRecordDO> pageResult = deviceRecordService.getDeviceRecordPage(pageVO);
        if (CollectionUtils.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }

        // 拼接结果返回
        List<MemberUserDO> users = memberUserService.getUserList(
                convertSet(pageResult.getList(), MemberDeviceRecordDO::getUserId));
        return success(MemberDeviceRecordConvert.INSTANCE.convertPage(pageResult, users));
    }

}
